from django.shortcuts import render, get_object_or_404, redirect
from django.core.paginator import Paginator
from django.contrib import messages
from .models import Category, Solution, Insight, Event, Banner, Office, Contact
from .forms import ContactForm
from django.http import Http404

def home(request):
    """首页视图"""
    banners = Banner.objects.filter(is_active=True).order_by('order')
    featured_solutions = Solution.objects.filter(is_featured=True).order_by('order')[:3]
    featured_insights = Insight.objects.filter(is_featured=True).order_by('-publication_date')[:3]
    upcoming_events = Event.objects.filter(is_featured=True).order_by('event_date')[:3]
    
    context = {
        'banners': banners,
        'featured_solutions': featured_solutions,
        'featured_insights': featured_insights,
        'upcoming_events': upcoming_events,
    }
    return render(request, 'core/home.html', context)

def company_intro(request):
    """公司介绍视图"""
    offices = Office.objects.all()
    context = {
        'offices': offices,
    }
    return render(request, 'core/company_intro.html', context)

def consulting_product(request, product_type):
    """咨询产品视图"""
    product_types = {
        'strategic_planning': '战略规划系统',
        'organization_intelligence': '组织智能系统',
        'position_optimization': '岗位优化系统',
        'salary_incentive': '薪酬激励系统',
        'performance_improvement': '绩效提升系统'
    }
    
    product_name = product_types.get(product_type, '战略规划系统')
    
    # 尝试从解决方案中查找相关内容
    solutions = Solution.objects.filter(title__icontains=product_name)
    
    context = {
        'product_type': product_type,
        'product_name': product_name,
        'solutions': solutions,
    }
    return render(request, 'core/consulting_product.html', context)

def classic_cases(request):
    """经典案例视图"""
    # 简化版本，无需动态数据和分页
    context = {}
    return render(request, 'core/classic_cases.html', context)

def solutions_list(request, category_slug=None):
    """解决方案列表视图"""
    category = None
    categories = Category.objects.filter(parent=None)
    solutions = Solution.objects.all().order_by('order')
    
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        solutions = solutions.filter(category=category)
    
    paginator = Paginator(solutions, 9)  # 每页显示9项
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    
    context = {
        'category': category,
        'categories': categories,
        'page_obj': page_obj,
    }
    return render(request, 'core/solutions_list.html', context)

def solution_detail(request, slug):
    """解决方案详情视图"""
    solution = get_object_or_404(Solution, slug=slug)
    related_solutions = Solution.objects.filter(category=solution.category).exclude(id=solution.id)[:3]
    
    context = {
        'solution': solution,
        'related_solutions': related_solutions,
    }
    return render(request, 'core/solution_detail.html', context)

def insights_list(request, category_slug=None):
    """洞察列表视图"""
    category = None
    categories = Category.objects.filter(parent=None)
    insights = Insight.objects.all().order_by('-publication_date')
    
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        insights = insights.filter(category=category)
    
    paginator = Paginator(insights, 9)  # 每页显示9项
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    
    context = {
        'category': category,
        'categories': categories,
        'page_obj': page_obj,
    }
    return render(request, 'core/insights_list.html', context)

def insight_detail(request, slug):
    """洞察详情视图"""
    insight = get_object_or_404(Insight, slug=slug)
    related_insights = Insight.objects.filter(category=insight.category).exclude(id=insight.id)[:3]
    
    context = {
        'insight': insight,
        'related_insights': related_insights,
    }
    return render(request, 'core/insight_detail.html', context)

def events_list(request):
    """活动列表视图"""
    events = Event.objects.all().order_by('event_date')
    
    paginator = Paginator(events, 9)  # 每页显示9项
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    
    context = {
        'page_obj': page_obj,
    }
    return render(request, 'core/events_list.html', context)

def event_detail(request, slug):
    """活动详情视图"""
    event = get_object_or_404(Event, slug=slug)
    related_events = Event.objects.exclude(id=event.id).order_by('event_date')[:3]
    
    context = {
        'event': event,
        'related_events': related_events,
    }
    return render(request, 'core/event_detail.html', context)

def contact(request):
    """咨询顾问视图"""
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, '您的信息已提交，我们将尽快与您联系。')
            return redirect('contact')
    else:
        form = ContactForm()
    
    offices = Office.objects.all()
    
    context = {
        'form': form,
        'offices': offices,
    }
    return render(request, 'core/contact.html', context)
